#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> pii;
const int N = 1010;
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
int n, m;
char a[N][N];
int dist[N][N];

void bfs()
{
    memset(dist, -1, sizeof(dist));
    queue<pii> as;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (a[i][j] == '1')
            {
                as.push({ i,j });
                dist[i][j] = 0;
            }
        }
    }
    while (as.size())
    {
        auto e = as.front();
        as.pop();
        int x = e.first;
        int y = e.second;
        for (int k = 0; k < 4; k++)
        {
            int s = x + dx[k];
            int f = y + dy[k];
            if (s >= 1 && s <= n && f >= 1 && f <= m && dist[s][f] == -1)
            {
                dist[s][f] = dist[x][y] + 1;
                as.push({ s,f });
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }
    bfs();
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cout << dist[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}